<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JHipsterRegistry - Swagger UI</title>
    <link rel="stylesheet" type="text/css" href="./swagger-ui.css">
    <link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32"/>
    <link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16"/>
</head>

<body>
<div id="swagger-ui"></div>

<script src="./swagger-ui-bundle.js"></script>
<script src="./swagger-ui-standalone-preset.js"></script>
<script src="./axios.min.js"></script>


<script type="text/javascript">
    window.onload = function () {

        function getCSRF() {
            var name = "XSRF-TOKEN=";
            var ca = document.cookie.split(';');
            for(var i=0; i<ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) === ' ') c = c.substring(1);
                if (c.indexOf(name) !== -1) return c.substring(name.length,c.length);
            }
            return "";
        }

        var urls = [];
        axios.get("/swagger-resources").then(function (response) {
            response.data.forEach(function (resource) {
                urls.push({"name": resource.name, "url": resource.location});
            });

            // Build a system
            var ui = SwaggerUIBundle({
                urls: urls,
                dom_id: '#swagger-ui',
                deepLinking: true,
                filter: true,
                layout: "StandaloneLayout",
                withCredentials: true,
                presets: [
                    SwaggerUIBundle.presets.apis,
                    SwaggerUIStandalonePreset
                ],
                plugins: [
                    SwaggerUIBundle.plugins.DownloadUrl
                ],
                requestInterceptor: function (req) {
                    return axios.get("/management/info").then(function(response) {
                        var oauth2 = false;
                        response.data.activeProfiles.forEach(function (activeProfile) {
                            if (activeProfile === 'oauth2') {
                                oauth2 = true;
                            }
                        });
                        if (oauth2) {
                            // OAuth2
                            req.headers['X-XSRF-TOKEN'] = getCSRF();
                        } else {
                            // working only with JWT authentication
                            var authToken = JSON.parse(localStorage.getItem("jhi-authenticationtoken")
                                || sessionStorage.getItem("jhi-authenticationtoken"));
                            if (authToken) {
                                console.log('hello');
                                req.headers['Authorization'] = "Bearer " + authToken;
                            }
                        }
                        return req;
                    });
                }
            });

            window.ui = ui
        });

    };
</script>
</body>
</html>
